<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="部门ID" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="部门ID"></el-input>
    </el-form-item> -->
    <el-form-item label="所属部门" prop="deptName">
      <el-popover
        ref="deptListPopover"
        placement="bottom-start"
        trigger="click">
        <el-tree
          :data="deptList"
          :props="deptListTreeProps"
          node-key="deptId"
          ref="deptListTree"
          @current-change="deptListTreeCurrentChangeHandle"
          :default-expand-all="true"
          :highlight-current="true"
          :expand-on-click-node="false">
        </el-tree>
      </el-popover>
      <el-input v-model="dataForm.deptName" v-popover:deptListPopover :readonly="true" placeholder="点击选择所属部门" class="menu-list__input"></el-input>
    </el-form-item>

    <el-form-item label="用户昵称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="用户邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="用户邮箱"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="用户性别" prop="sex">
      <el-select v-model="dataForm.sex" placeholder="用户性别">
        <el-option key="0" label="男" value="0"></el-option>
        <el-option key="1" label="女" value="1"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item v-if="dataForm.userId" label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item v-if="dataForm.userId" label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间" :disabled="true"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          deptId: '',
          userName: '',
          email: '',
          phone: '',
          sex: '',
          createTime: '',
          updateTime: '',
          deptName: ''
        },
        dataRule: {
          deptId: [
            { required: true, message: '部门ID不能为空', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '所属部门不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '用户邮箱不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '用户性别不能为空', trigger: 'blur' }
          ]
        },
        deptList: [],
        deptListTreeProps: {
          label: 'deptName',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        // 渲染部门信息
        this.$http({
          url: this.$http.adornUrl('/oa/dept/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.deptList = treeDataTranslate(data, 'deptId')
        }).then(() => {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (this.dataForm.userId) {
              this.$http({
                url: this.$http.adornUrl(`/oa/deptuser/info/${this.dataForm.userId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.deptId = data.deptUser.deptId
                  this.dataForm.userName = data.deptUser.userName
                  this.dataForm.email = data.deptUser.email
                  this.dataForm.phone = data.deptUser.phone
                  this.dataForm.sex = data.deptUser.sex
                  this.dataForm.createTime = data.deptUser.createTime
                  this.dataForm.updateTime = data.deptUser.updateTime
                  this.deptListTreeSetCurrentNode()
                }
              })
            }
          })
        })
      },
      // 菜单树选中
      deptListTreeCurrentChangeHandle (data, node) {
        this.dataForm.deptId = data.deptId
        this.dataForm.deptName = data.deptName
      },
      // 菜单树设置当前选中节点
      deptListTreeSetCurrentNode () {
        this.$refs.deptListTree.setCurrentKey(this.dataForm.deptId)
        this.dataForm.deptName = (this.$refs.deptListTree.getCurrentNode() || {})['deptName']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/oa/deptuser/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'deptId': this.dataForm.deptId,
                'userName': this.dataForm.userName,
                'email': this.dataForm.email,
                'phone': this.dataForm.phone,
                'sex': this.dataForm.sex
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
